#include "weather.h"
#include "ui_weather.h"
#include<QNetworkAccessManager>//网络连接管理
#include<QFileDialog>
#include<QIODevice>
#include<QNetworkRequest>
#include<QNetworkReply>
#include<QDebug>
#include<QJsonParseError>
#include<QJsonDocument>
#include<QJsonObject>
#include<QJsonValue>

weather::weather(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::weather)
{
    ui->setupUi(this);
    setMouseTracking(true);
    this->setWindowFlags(Qt::FramelessWindowHint);
    this->setAttribute(Qt::WA_TranslucentBackground);

    manager=new QNetworkAccessManager(this);
    connect(manager,&QNetworkAccessManager::finished,this,&weather::replyFinished);

    this->getreq();


}

weather::~weather()
{
    delete ui;
}
void weather::getreq()
{
    //通过连接的请求是要访问到特定的网址接口
    QNetworkRequest request;
    request.setUrl(QUrl("https://v0.yiketianqi.com/api?unescape=1&version=v61&appid=86887989&appsecret=Ayr4k6iv"));
    manager->get(request);//manager要发出一个get请求

}
void weather::replyFinished(QNetworkReply *rpl){
    QByteArray str;//字符数组
    str = rpl->readAll();
    this->jsonanaly(str);
}
void weather::jsonanaly(QByteArray str)
{
    QJsonParseError error;
    QJsonDocument document=QJsonDocument::fromJson(str,&error);
    QJsonObject ob=document.object();
    if(error.error==QJsonParseError::NoError){
        QJsonValue city=ob.value("city");
        citystr=city.toString();
        QJsonValue date=ob.value("date");
        datestr=date.toString();
        QJsonValue week=ob.value("week");
        weekstr=week.toString();
        QJsonValue wea=ob.value("wea");
        weastr=wea.toString();
        QJsonValue tem=ob.value("tem");
        temstr=tem.toString();
        QJsonValue temhigh=ob.value("tem1");
        temhighstr=temhigh.toString();
        QJsonValue temlow=ob.value("tem2");
        temlowstr=temlow.toString();
        QJsonValue win_v=ob.value("win_meter");
        win_vstr=win_v.toString();//风速
        QJsonValue win=ob.value("win");
        winstr=win.toString();//风向
        QJsonValue win_speed=ob.value("win_speed");
        win_speedstr=win_speed.toString();//风力等级

        if(weastr=="晴"){
            ui->bk->setPixmap(QPixmap("://image/sunshine_pic.png"));
            ui->bk->setScaledContents(true);
            ui->weatherpic->setPixmap(QPixmap(":/image/sunshine.png"));
            ui->weatherpic->setScaledContents(true);
        }
        if(weastr=="多云"){
            ui->bk->setPixmap(QPixmap("://image/cloudy_pic.png"));
            ui->bk->setScaledContents(true);
            ui->weatherpic->setPixmap(QPixmap("://image/cloudy.png"));
            ui->weatherpic->setScaledContents(true);
        }
        if(weastr=="小雨"){
            ui->bk->setPixmap(QPixmap("://image/windy_pic.png"));
            ui->bk->setScaledContents(true);
            ui->weatherpic->setPixmap(QPixmap("://image/cloud_and_rain.png"));
            ui->weatherpic->setScaledContents(true);
        }
        if(weastr=="中雨"){
            ui->bk->setPixmap(QPixmap("://image/smallrain_pic.png"));
            ui->bk->setScaledContents(true);
            ui->weatherpic->setPixmap(QPixmap("://image/smallrain.png"));
            ui->weatherpic->setScaledContents(true);
        }
        if(weastr=="大雨"){
            ui->bk->setPixmap(QPixmap("://image/bigrain_pic.png"));
            ui->bk->setScaledContents(true);
            ui->weatherpic->setPixmap(QPixmap("://image/bigrain.png"));
            ui->weatherpic->setScaledContents(true);
        }
        if(weastr=="雪"){
            ui->bk->setPixmap(QPixmap("://image/snowy_pic.png"));
            ui->bk->setScaledContents(true);
            ui->weatherpic->setPixmap(QPixmap("://image/snowy.png"));
            ui->weatherpic->setScaledContents(true);
        }
        ui->templow->setText(temlowstr+"°");
        ui->temphigh->setText(temhighstr+"°");
        ui->temp->setText(temstr+"°");
        ui->week->setText(weekstr);
        ui->date->setText(datestr);
        ui->location->setText(citystr);
        ui->windv->setText(win_vstr);
        ui->weastate->setText(weastr);
        ui->direction->setText(winstr);
        ui->degree->setText(win_speedstr);

    }

}

void weather::on_close_clicked()
{
    this->close();
}

void weather::on_min_clicked()
{
    this->showMinimized();
}
void weather::mousePressEvent(QMouseEvent *e){
    if(e->button()==Qt::LeftButton){
        p=e->globalPos()-this->frameGeometry().topLeft();
    }
    else if(e->button()==Qt::RightButton){
        return;
    }
}
void weather::mouseMoveEvent(QMouseEvent *e){
    if(e->buttons()==Qt::LeftButton){//获取的长时间的按键是左键
        this->move(e->globalPos()-p);
    }
}
